/*
 * Copyright 2022-2023 the original author(https://github.com/wj596)
 * 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */
package org.jsets.fastboot.flowable.config;

import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.jsets.fastboot.flowable.component.CustomProcessDiagramGenerator;
import org.jsets.fastboot.util.SpringContextHolder;
import org.springframework.context.annotation.Configuration;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Flowable 配置
 * @author wj596
 *
 */
@Slf4j
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
	
    public static final String PROCESS_DEF_SUFFIX = ".bpmn20.xml";// 流程文件后缀
    public static final String PROCESS_DEF_SUFFIX_BPMN = ".bpmn";// 流程文件后缀
    public static final String ZIP_FILE_SUFFIX = ".zip";//压缩包后缀
    public static final String BASE64_IMG_HEAD = "data:image/png;base64,";//base图片头
    public static final String PROCESS_IMG_SUFFIX = ".png";//流程图后缀
    public static final String PROCESS_IMG_FONT = "宋体";//流程图字体
    public static final String PROCESS_START_USER_KEY = "startUser"; // 流程发起人
    public static final String PROCESS_LAST_ASSIGNEE_KEY = "_lastAssignee"; // 上一步Assignee
    public static final String HANDLE_RESULT_KEY = "handleResult";// 处理结果属性名称
    public static final String HANDLE_RESULT_PASS = "pass";// 处理结果-通过
    public static final String HANDLE_RESULT_REJECT = "reject";// 处理结果-驳回
    public static final String ACTIVITY_TYPE_SEQUENCE_FLOW = "sequenceFlow";
    public static final String ACTIVITY_TYPE_EXCLUSIVE_GATEWAY = "exclusiveGateway";
    public static final String ACTIVITY_TYPE_USER_TASK = "UserTask";
    public static final String ACTIVITY_TYPE_END_EVENT = "EndEvent";
    public static final Integer COMPLETE_TYPE_MANUAL = 0;
    public static final Integer COMPLETE_TYPE_AUTO = 1;
    
    
    @Override
    public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
    	processEngineConfiguration.setActivityFontName(PROCESS_IMG_FONT);
    	processEngineConfiguration.setLabelFontName(PROCESS_IMG_FONT);
    	processEngineConfiguration.setAnnotationFontName(PROCESS_IMG_FONT);
        processEngineConfiguration.setXmlEncoding(StandardCharsets.UTF_8.toString());
        
        SpringContextHolder.tryBeansThen(FlowableEventListener.class, map -> {
        	List<FlowableEventListener> eventListeners = new ArrayList<FlowableEventListener>();
        	map.values().forEach(l->eventListeners.add(l));
            processEngineConfiguration.setEventListeners(eventListeners);
        });
    	processEngineConfiguration.setProcessDiagramGenerator(new CustomProcessDiagramGenerator());
    }
}